<template>
  <el-dialog :title="$t('station.selectDevices')" :visible.sync="visible" width="1200px" top="5vh" append-to-body>
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item :label="$t('station.deviceName')" prop="name">
        <el-input
          v-model="queryParams.name"
          :placeholder="$t('station.deviceName')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('common.search')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('common.reset')}}</el-button>
        <el-button icon="el-icon-plus" size="mini" @click="handleSelect">{{$t('common.add')}}</el-button>
      </el-form-item>
      <!-- <el-form-item style="float: right">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="createNewMedia" >新增媒体文件</el-button>
      </el-form-item> -->
    </el-form>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="mediaList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" :selectable="handleSelectable"/>
      <el-table-column :label="$t('station.deviceName')" align="center" prop="name" />
      <el-table-column :label="$t('station.deviceCode')" align="center" prop="code" />
      <el-table-column :label="$t('station.address')" align="center" prop="address" />
      <el-table-column :label="$t('station.deviceState')" align="center" prop="inline" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.online_status" :value="scope.row.inline"/>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <div style="margin: 20px 0 10px">
      {{$t('station.selectedDevices')}}：
      <div style="margin: 10px 0">
        <el-tag
          style="margin-right: 10px;"
          v-for="tag in rtnMedias"
          :key="tag.name"
          closable
          @close="closeTag"
          type="info">
          {{tag.name}}
        </el-tag>
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handleSelectDevice">{{$t('common.confirm')}}</el-button>
      <el-button @click="visible = false">{{$t('common.cancel')}}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { managerList} from "@/api/device/device";
  import { getToken } from '@/utils/auth'
  export default {
    props:{
      companyId:''
    },
    name:'selectDevice',
    dicts: ['online_status'],
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // 表格树数据
        mediaList: [],
        rtnMedias:[],
        selectorList:[],
        // 弹出层标题
        title: "",
        visible:false,
        // 是否显示弹出层
        open: false,
        // 重新渲染表格状态
        refreshTable: true,
        ids:null,
        // 查询参数
        queryParams: {
          name: '',
          companyId:this.companyId,
          pageNum: 1,
          pageSize: 10,
        },
        // 总条数
        total: 0,
        // 表单参数
        form: {},
        fileList:[],
        rules: {
        },
        token:''
      };
    },
    created() {
      this.token = 'Bearer ' + getToken();
    },
    computed:{
      isSelected(){
        return (id)=>{
          let index = this.rtnMedias.findIndex(i=>i.id===id);
          if(index>-1)return true;
          return  false;
        }
      }
    },
    methods: {
      // 显示弹框
      show() {
        this.getList();
        this.rtnMedias = []
        this.visible = true;
      },
      /** 查询流媒体列表 */
      getList() {
        this.loading = true;
        managerList(this.queryParams).then(response => {
          this.mediaList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },

      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleSelectDevice(){
        if(this.rtnMedias.length<=0)
        {
          this.$modal.msgError("最少选择一行数据");
          return;
        }
        this.$emit("ok",this.rtnMedias)
        this.visible = false
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.selectorList = selection
      },
      handleSelect(){
        this.rtnMedias = this.rtnMedias.concat(this.selectorList)
      },
      closeTag(item){
        let index = this.rtnMedias.findIndex(i=>i.id===item.id);
        this.rtnMedias.splice(index,1)
      },
      handleSelectable(row){
        return (row.stationId == 0 || !row.stationId);
      }
    }
  };
</script>
